var yhmFlag = 0,
    mmFlag = 0;

// 拼后台传过来的listjson并插入到表格中
function updateList(data){
    var dataL = data.length;
    var html = "";
    for(var i = 0; i < dataL; i++){
        html = html + "<tr>"
            +  "<td>" + zhigong.undefinedHandler(data[i].yhm) + "</td>"
            +  "<td>" + zhigong.undefinedHandler(data[i].ygxm) + "</td>"
            +  "<td>" + zhigong.undefinedHandler(data[i].roleName) + "</td>"
            +  "<td>"
            +  "<a href=\"javascript:zhigong.openModal('yonghu','view','" + data[i].yhm  +"')\" class=\"btn btn-primary btn-xs btn-margin\" >查看</a>"
            +  "<a href=\"javascript:zhigong.openModal('yonghu','update','" + data[i].yhm + "')\" class=\"btn btn-info btn-xs btn-margin\">修改</a>"
            +  "<a href=\"javascript:zhigong.deleteData('yonghu', '" + data[i].yhm + "')\" class=\"btn btn-danger btn-xs btn-margin\">删除</a>"
            +  "</td>"
            +"</tr>";
    }

    var tbodyList = $("#tbodyList");
    tbodyList.html(html);
}

// 用户模块自己的保存逻辑，类似common但无法完全共同使用

function saveYonghu (){
    var module = "yonghu";

    // 获取上报数据,json字符串格式.
    var inputNodes = $('#dataModal').find('.form-control'),
        inputNodesL = inputNodes.length,
        data = "{";
    for(var i = 0; i < inputNodesL; i++){
        data = data + '"' + inputNodes[i].id + '":"' + $("#"+inputNodes[i].id).val() + '",';
    }
    data = data.slice(0,-1);
    data = data + "}";

    // 默认执行新增的保存
    var status = "POST";
    var ajaxUrl = zhigong.URL.urlNoParam(module);
    // 取主键的disabled属性，如果没有则说明是新增
    var prikey = $(".prikey");
    // 类中有主键类并且主键类的元素中的第一个的值不为空,代表是更新操作
    if(typeof(prikey.attr("disabled")) != "undefined") {
        var paramArr = [];
        for(var i = 0; i < prikey.length; i++) {
            paramArr[i] = prikey.eq(i).val();
        }
        status = "PUT";
        ajaxUrl = zhigong.URL.urlWithParam(module, paramArr);
    }

    $.ajax({
        url : ajaxUrl,
        type : status,
        dataType:"json",
        contentType : "application/json;charset=UTF-8",
        data : data,
        success:function(result) {
            var message= JSON.stringify(result);
            console.log("success:"+message);

            // 显示保存成功
            $("#alertMsg").html(result.data == 1 ? "保存成功" : "保存失败").addClass('alert-success').addClass('in');

            // 关闭模态框
            $('#dataModal').modal('hide');

            // 一秒后刷新页面
            setTimeout(function(){
                $("#alertMsg").removeClass('in');
                window.location.reload();
            },1000);
        },
        error:function(result){
            var message= JSON.stringify(result);
            console.log("error:"+message);
            $("#alertMsg").html(result.error).addClass('alert-danger').addClass('in');
        }
    });
}


$(function(){
    // 返回角色列表
    $.ajax({
        url: zhigong.URL.urlNoParam('role'),
        type: "GET",
        dataType: "json",
        data: {
            pageNumber : -1
        },
        contentType: "application/json;charset=UTF-8",
        success: function (result) {

            var message = JSON.stringify(result);
            console.log("role success:" + message);

            var resData = result.data;
            var dataL = resData.length;
            var html = "";
            for(var i = 0; i < dataL; i++){
                html = html + "<option value="+ resData[i].id +">"+ resData[i].name +"</option>>";
            }
            $("#roleId").html(html);
        },
        error: function (result) {
            var message = JSON.stringify(result);
            console.log("error:" + message);
        }
    })

    // 初始化加载列表数据
    zhigong.pageData("yonghu", 1, true);

    $("#yhm").on('blur',function(){
        if($("#yhm").val()==''){
            $("#yhmVarify").html("<span class='label label-danger'>用户名不能为空！</span>");
            yhmFlag = 1;
            $("#saveBtn").attr("disabled","disabled");
            return;
        }
        // 验证用户是否已存在
        $.ajax({
            url: "yonghus/"+$("#yhm").val()+"/varify",
            type: "GET",
            dataType: "json",
            contentType: "application/json;charset=UTF-8",
            success: function (result) {

                var message = JSON.stringify(result);
                console.log("varigy success:" + message);

                var resData = result.data;
                if(resData == 1){
                    $("#yhmVarify").html("<span class='label label-danger'>用户名已存在！</span>");
                    yhmFlag = 1;
                    $("#saveBtn").attr("disabled","disabled");
                }else{
                    $("#yhmVarify").html("<span class='label label-success'>用户名可用！</span>");
                    yhmFlag = 0;
                    if(mmFlag == 0){
                        $("#saveBtn").removeAttr("disabled");
                    }
                }
            },
            error: function (result) {
                var message = JSON.stringify(result);
                console.log("error:" + message);
            }
        })
    });
    $("#mm").on('blur',function(){
        if($("#mm").val() == ''){
            $("#mmVarify").html("<span class='label label-danger'>密码不能为空！</span>");
            $("#saveBtn").attr("disabled","disabled");
            mmFlag = 1;
            return;
        }else{
            if($("#mm").val() != $("#mm2").val()){
                $("#mmVarify").html("<span class='label label-danger'>密码不一致！</span>");
                mmFlag = 1;
                $("#saveBtn").attr("disabled","disabled");
            }else{
                $("#mmVarify").html("<span class='label label-success'>密码一致！</span>");
                mmFlag = 0;
                if(yhmFlag == 0){
                    $("#saveBtn").removeAttr("disabled");
                }
            }
        }

    });
    $("#mm2").on('blur',function(){
        if($("#mm2").val() == ''){
            $("#mmVarify").html("<span class='label label-danger'>确认密码不能为空！</span>");
            $("#saveBtn").attr("disabled","disabled");
            mmFlag = 1;
            return;
        }else {
            if ($("#mm").val() != $("#mm2").val()) {
                $("#mmVarify").html("<span class='label label-danger'>密码不一致！</span>");
                mmFlag = 1;
                $("#saveBtn").attr("disabled", "disabled");
            } else {
                $("#mmVarify").html("<span class='label label-success'>密码一致！</span>");
                mmFlag = 0;
                if(yhmFlag == 0){
                    $("#saveBtn").removeAttr("disabled");
                }
            }
        }
    });

    // 为模态窗绑定隐藏事件,隐藏时将input设为可用,将保存按钮显示.
    $('#dataModal').on('hidden.bs.modal',function () {
        $('#dataModal').find('input').val("").removeAttr("disabled");
        $('#dataModal').find('select').removeAttr("disabled");
        $("#saveBtn").removeAttr("disabled");
        $('#saveBtn').show();
        $("#yhmVarify").html("");
        $("#mmVarify").html("");
        yhmFlag = 0;
        mmFlag = 0;
    });
})